/**
 * 
 */
package es.twentymobile.mule.modules.jpa.xa;

import javax.persistence.EntityManager;

import org.mule.api.transaction.TransactionException;
import org.mule.transaction.XaTransaction.MuleXaObject;

/**
 * @author alvarezjm
 * 
 */
public class JpaXaResource implements MuleXaObject {

	protected EntityManager impl;

	public EntityManager getEntityManager() {
		return impl;
	}

	public JpaXaResource(EntityManager s) {
		this.impl = s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mule.transaction.XaTransaction.MuleXaObject#close()
	 */
	public void close() throws Exception {
		closeEntityManager(impl);
	}

	protected void closeEntityManager(EntityManager entityManager) {
		if (entityManager != null && entityManager.isOpen()) {
			entityManager.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.mule.transaction.XaTransaction.MuleXaObject#setReuseObject(boolean)
	 */
	@Override
	public void setReuseObject(boolean reuseObject) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mule.transaction.XaTransaction.MuleXaObject#isReuseObject()
	 */
	@Override
	public boolean isReuseObject() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mule.transaction.XaTransaction.MuleXaObject#enlist()
	 */
	@Override
	public boolean enlist() throws TransactionException {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mule.transaction.XaTransaction.MuleXaObject#delist()
	 */
	@Override
	public boolean delist() throws Exception {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mule.transaction.XaTransaction.MuleXaObject#getTargetObject()
	 */
	@Override
	public Object getTargetObject() {
		return impl;
	}

}
